<?php
namespace app\controller\demo;

use app\facade\MobileDetect;
use app\facade\QrCode;
use Gregwar\Captcha\CaptchaBuilder;
use think\Facade;
use think\facade\Session;
use think\Request;
use Intervention\Image\ImageManagerStatic as Image;

class Normal extends Application
{
    public function initialize()
    {

    }

    public function getView($openid = 'UNZ0Haq0fVuiHpiTY/HQU')
    {
        $this->assign('openid', $openid);
        return view('tpl', ['name' => 'ouJian']);
    }

    public function getView2()
    {
        return $this->fetch('demo/other/view2');
    }

    /**
     * 中间跳转页
     */
    public function getJump()
    {
        $this->success('新增成功', 'demo/view');
    }

    /**
     * 传参跳转
     */
    public function getJump2()
    {
        $this->redirect('demo/view', ['openid' => 12345678]);
    }

    /**
     * services类注册使用
     */
    public function getServ()
    {
        session('name','thinkphp');
        dump(sys_get_temp_dir());
    }

    public function getHeader()
    {
        dump($this->request->header());
    }

    /**
     * mobile环境检测
     */
    public function getDetect()
    {
        dump(MobileDetect::getBrowsers());
        dump(MobileDetect::isMobile());
    }

    /**
     * 图片处理
     * @return $this
     */
    public function getImage()
    {
        //dd(file_exists(app()->getRootPath().'public/image/test.jpg'));
        $img = Image::make(app()->getRootPath().'public/image/test.jpg')->resize(50, 50);
        return response()->data($img->response('jpg'))->contentType('image/jpeg');
    }

    /**
     * 生成图片验证码
     * @return $this
     */
    public function getCaptcha()
    {
        $builder = CaptchaBuilder::create();
        $builder->build();
        Session::set('captcha_phrase', $builder->getPhrase());
        return response()->data($builder->output())->contentType('image/jpeg');
    }

    /**
     * 获取验证码字符串
     */
    public function getPhrase()
    {
        //Session::flush();
        //dd(Session::get('captcha_phrase'));
        /** 一次性取出并删除 */
       dd(Session::pull('captcha_phrase'));
    }

    public function getQrcode()
    {
        $qrCode = QrCode::format('png')->size(100)->margin(2)->generate('hello world');
        return response($qrCode)->contentType('image/png');
    }

    /**
     * 使用自定义路由后，失效
     * @return array
     */
    public function _empty()
    {
        return $this->formatResult(-1, 'action not found');
    }
}
